package easybooking;

import easybooking.db.AirlineMapper;
import easybooking.db.AirportMapper;
import easybooking.db.BookingMapper;
import easybooking.db.CustomerMapper;
import easybooking.db.FlightMapper;
import easybooking.db.PlaneMapper;
import easybooking.db.SeatMapper;
import easybooking.db.TypeMapper;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.8349C54C-FA1D-4335-3668-02697FAF837C]
// </editor-fold> 
public class FlightServer {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.A5917095-422F-E224-EC9C-DAF890AE9DBE]
    // </editor-fold> 
    private FlightManagement management;
    private String rmiHost = "localhost";

    public String getRmiHost() {
        return rmiHost;
    }

    public void setRmiHost(String rmiHost) {
        this.rmiHost = rmiHost;
    }
    private Registry registry;
    private FlightServerController fsc;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.D9A9DFE7-6621-92DA-D247-F3A574327BF8]
    // </editor-fold> 
    public FlightServer(FlightServerController fsc) {
        try {
            this.fsc = fsc;

            management = new FlightManagementImpl(BookingMapper.bookingMapper(), FlightMapper.flightMapper(), CustomerMapper.customerMapper(), AirportMapper.airportMapper(), PlaneMapper.planeMapper(), AirlineMapper.airlineMapper(), TypeMapper.typeMapper(), SeatMapper.seatMapper());

            registry = LocateRegistry.createRegistry(1099);
            Naming.rebind("rmi://localhost:1099/FlightManagement1", management);


            fsc.getServerGui().getTextArea().append("FlightServer registrated in RMI-Registry\n");
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets the flight management object.
     * @return the Flight Management object
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.2BD77E47-FB28-47B6-82F9-41EAFB6F0E0A]
    // </editor-fold> 
    public FlightManagement getManagement() {
        return management;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.CC6BFE11-B692-6C7A-75B3-1EA9AD637F0F]
    // </editor-fold> 
    public void setManagement(FlightManagement val) {
        this.management = val;
    }

    @Override
    public void finalize() {
        try {
            try {
                super.finalize();
            } catch (Throwable ex) {
                Logger.getLogger(FlightServer.class.getName()).log(Level.SEVERE, null, ex);
            }
            Naming.unbind("rmi://localhost:1099/FlightManagement1");
            if (registry != null) {
                UnicastRemoteObject.unexportObject(registry, true);
            }

        } catch (NotBoundException ex) {
           // Logger.getLogger(FlightServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
           // Logger.getLogger(FlightServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
           // Logger.getLogger(FlightServer.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}


